<?php $memeUrl = $meme->post_url ?>
<div class="corgi_feed_well">
    <div class="feed_stacked">
        <div class="feed_item meme">
            <div class="feed_body">
                <div class="row">
                    <div class="feed_profile_pic">
                        <a href="<?php echo Yii::app()->createUrl('site/profile', array('profile' => $meme->user->username)) ?>"><img src="<?php echo Yii::app()->user->getAvatar_url($meme->user_fk) ?>" alt="<?php echo CHtml::encode("{$meme->user->first_name} {$meme->user->last_name}") ?>" title="<?php echo CHtml::encode("{$meme->user->first_name} {$meme->user->last_name}") ?>" class="meta_image ttip" /></a>
                    </div>
                    <span class="timesago"><?php echo Yii::app()->format->formatTimeago($meme->created_at) ?></span>
                    <div class="feed_text text-center genFile">
                        <h3><a href="<?php echo $memeUrl ?>"><?php echo CHtml::encode($meme->title) ?></a></h3>
                        <?php $isGif = ''; if (!preg_match('/^http/i', $meme->file)) {
                                // changed v2.16
                                $isGif = substr($meme->file, -3) == 'gif';
                                if($single):
                            ?>
                                <a href="<?php echo $memeUrl ?>"><img class="meme-img" src="<?php echo $isGif ? $meme->url_orignal : $meme->url ?>" alt="<?php echo CHtml::encode($meme->title) ?>" /></a>
                            <?php else: ?>
                                <a href="<?php echo $memeUrl ?>" class="<?php echo $isGif ? 'play-gif' : '' ?>" style="position:relative;">
                                    <img class="meme-img" data-src-orignal="<?php echo $meme->url_orignal ?>" src="<?php echo $meme->url ?>" alt="<?php echo CHtml::encode($meme->title) ?>" />
                                    <?php if($isGif): ?>
                                        <i class="icon-play" style="color: rgba(0, 0, 0, 0.3);font-size: 70px;left: 40%;position: absolute;text-decoration: none;top: -100%;"></i>
                                    <?php endif ?>
                                </a>
                            <?php endif ?>
                        <?php } else {
                            $url = $meme->file;
                            // we get the unique video id from the url by matching the pattern
                            preg_match("/v=([^&]+)/i", $url, $matches);
                            $name = $meme->title;
                            $id = $matches[1];

                            // this is your template for generating embed codes
                            $code = '<object title="' . $name . '"><param name="movie" value="http://www.youtube.com/v/{id}&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/{id}&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true"></embed></object>';

                            // we replace each {id} with the actual ID of the video to get embed code for this particular video
                            echo $code = str_replace('{id}', $id, $code);
                        } ?>
                        <div class="social">
                            <a href="javascript:;" class="favourite <?php echo $meme->checkUserLike($meme->meme_id, Yii::app()->user->id) ? 'useful' : ''; ?>" title="<?php echo $meme->title; ?>" data-meme-id="<?php echo $meme->meme_id ?>">
                                <img src="<?php echo Yii::app()->theme->baseUrl; ?>/img/facebook_24.png" alt='Like my site' /><span><?php echo $meme->likes_count; ?></span>
                            </a>
                            <div class="fb-like clearfix" data-href="<?php echo $memeUrl ?>" data-layout="standard" data-action="like" data-show-faces="false" data-share="true"  ></div>
                            <div class="fb-comments clearfix" data-href="<?php echo $memeUrl ?>" data-width="500" data-numposts="5" data-colorscheme="light" > </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
$url = Yii::app()->createUrl('/site');
$urlLogin = Yii::app()->createUrl('/site/login');
$checkUserLogin = Yii::app()->user->isGuest;
$js = <<<JS
    $('.favourite').click(function(){
        if ("$checkUserLogin" == "") {
            var self = $(this);
            var isLiked = self.hasClass('useful');
            var meme_id = self.data('meme-id');
            var url = '$url' + (isLiked ? '/memeunlike' : '/memelike');
            var spanValue = $(this).find('span');
            var numberLike = isLiked ? parseInt(spanValue.text()) - 1 : parseInt(spanValue.text()) + 1;

            $.ajax({
                url: url,
                type: 'post',
                data: { id: meme_id},
                beforeSend: function() {
                    $('.loading').toggle();
                },
                success: function(data){
                    $('.loading').toggle();
                    self.toggleClass('useful');
                    spanValue.text(numberLike);
                }
            });
        } else {
            window.location.href = '{$urlLogin}';
        }

        return false;
    });
JS;
Yii::app()->clientScript->registerScript('like-btn', $js, CClientScript::POS_END);